package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.product.PrPreterminationFeeTableReq;
import com.jrx.anytxn.param.dto.product.PrPreterminationFeeTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IPrPreterminationFeeTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 提前结清费表
 *
 * @author Yangxin
 * @date 2019/8/2
 **/
@Api(tags = "页面提前结清费参数API接口")
@RestController
@RequestMapping(value = "/param/preterminationFees")
public class PreterminationFeeTableController {
    private static final Logger logger = LoggerFactory.getLogger(PreterminationFeeTableController.class);
    @Autowired
    IPrPreterminationFeeTableService iPrPreterminationFeeTableService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和费用表ID查询提前结清费表信息", notes = "根据租户ID和费用表ID查询提前结清费表信息")
    @GetMapping(value = "/{tenantId}/{feeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "feeId", value = "费用表ID", required = true)
    })
    public TxnRespResult<PrPreterminationFeeTableRes> getById(@NotNull @PathVariable String tenantId, @PathVariable String feeId) {
        TxnRespResult<PrPreterminationFeeTableRes> result = new TxnRespResult<>();
        PrPreterminationFeeTableRes res;
        try {
            res = iPrPreterminationFeeTableService.findByTenIdAndFeeId(String.valueOf(infoService.getUser().getProjectId()), feeId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID分页查询提前结清费表信息", notes = "根据租户ID分页查询提前结清费表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true),})
    public TxnRespResult<TxnPage<PrPreterminationFeeTableRes>> getAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {

        TxnRespResult<TxnPage<PrPreterminationFeeTableRes>> result = new TxnRespResult<>();
        TxnPage<PrPreterminationFeeTableRes> response;
        try {
            String tenantId= String.valueOf(infoService.getUser().getProjectId());
            response = iPrPreterminationFeeTableService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "新增提前结清费表信息", notes = "新增提前结清费表信息")
    @PostMapping("/")
    public TxnRespResult<PrPreterminationFeeTableRes> createPreterminationFeeTable(@RequestBody PrPreterminationFeeTableReq prPreterminationFeeTableReq) {
        PrPreterminationFeeTableRes res;
        TxnRespResult<PrPreterminationFeeTableRes> result = new TxnRespResult<>();
        try {
            prPreterminationFeeTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prPreterminationFeeTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iPrPreterminationFeeTableService.addPreterminationFeeTable(prPreterminationFeeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑提前结清费表信息", notes = "编辑提前结清费表信息")
    @PutMapping("/")
    public TxnRespResult<PrPreterminationFeeTableRes> modifyPreterminationFeeTable(@RequestBody PrPreterminationFeeTableReq prPreterminationFeeTableReq) {
        TxnRespResult<PrPreterminationFeeTableRes> result = new TxnRespResult<>();
        PrPreterminationFeeTableRes res;
        try {
            prPreterminationFeeTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iPrPreterminationFeeTableService.modifyPreterminationFeeTable(prPreterminationFeeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除提前结清费表信息", notes = "根据id删除提前结清费表信息")
    @DeleteMapping(value = "/{tenantId}/{feeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "feeId", value = "费用表ID", required = true)
    })
    public TxnRespResult<Boolean> removeId(@NotNull @PathVariable String tenantId, @PathVariable String feeId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iPrPreterminationFeeTableService.removeByTenIdAndFeeId(String.valueOf(infoService.getUser().getProjectId()), feeId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


}
